'use strict';
angular.module('myApp.robot', ['ngRoute'])
    .config(['$routeProvider', function($routeProvider) {
        $routeProvider.when('/robot', {
            templateUrl: 'view/robot/robot.html',
            controller: 'RobotCtrl'
        });
    }])
    .controller('RobotCtrl', function($scope,$http,$interval) {
        $scope.dialogue=[];
        $scope.enter=function (e) {
            var keycode = window.event?e.keyCode:e.which;
            if(keycode==13&&!e.ctrlKey){
                if($scope.info.length>0&&$scope.info!=""){
                    var ask={"name":"anyme","img":"static/img/anyme.png","isUser":true,"info":$scope.info,"time":new Date()};
                    $scope.dialogue.push(ask);
                    $scope.info="";
                    $http({
                        method:'post',
                        url:"http://www.tuling123.com/openapi/api",
                        params:{"key":"9d00382eb4b64014a93cf206cb496ab7","info":ask.info,"userid":ask.name}
                    }).then(function successCallback(response){
                        var ans={"name":"Sirilly","img":"static/img/robot.jpg","isUser":false,"info":response.data.text,"url":response.data.url,"list":response.data.list,"time":new Date()};
                        $scope.dialogue.push(ans);
                        console.log(response.data)
                    });
                }
            }
        };
        $scope.preDefult=function (e) {
            var keycode = window.event?e.keyCode:e.which;
            if(keycode == 13 && e.ctrlKey){
                $scope.info += "\n";
            }else if(keycode == 13){
                e.preventDefault();
            }

        };
        /**
         * 当点击聊天记录窗口时,不让滚动条自动滚至底部
         * @type {boolean}
         */
        //标示滚动条是否滑至底部
        /*$scope.isAttached   = false;
        $scope.unbindGlued = function(){
            $scope.isAttached   = true;
        };
        /!**
         * 聊天记录窗口失去焦点时,让滚动条自动滚至底部
         * @type {boolean}
         *!/
        $scope.bindGlued = function(){
            $scope.isAttached   = false;
        };*/
    }).directive('scrollToBottom', ['$parse', '$window', '$timeout', function ($parse, $window, $timeout){
    function createActivationState($parse, attr, scope) {
        function unboundState(initValue) {
            var activated = initValue;
            return {
                getValue: function () {
                    return activated;
                },
                setValue: function (value) {
                    activated = value;
                }
            };
        }
        function oneWayBindingState(getter, scope){
            return {
                getValue: function () {
                    return getter(scope);
                },
                setValue: function () {
                }
            };
        }
        function twoWayBindingState(getter, setter, scope) {
            return {
                getValue: function () {
                    return getter(scope);
                },
                setValue: function (value) {
                    if (value !== getter(scope)) {
                        scope.$apply(function () {
                            setter(scope, value);
                        });
                    }
                }
            };
        }
        if (attr !== '') {
            var getter = $parse(attr);
            if (getter.assign !== undefined) {
                return twoWayBindingState(getter, getter.assign, scope);
            } else {
                return oneWayBindingState(getter, scope);
            }
        } else {
            return unboundState(true);
        }
    }
    return {
        priority: 1,
        restrict: 'A',
        link: function (scope, $el, attrs) {
            var el = $el[0],
                activationState = createActivationState($parse, attrs.scrollToBottom, scope);

            var bottom = {
                isAttached: function (el,isAttached) {
                    return isAttached;
                },
                scroll: function (el) {
                    el.scrollTop = el.scrollHeight;
                }
            };
            function scrollIfGlued() {
                if (activationState.getValue() && !bottom.isAttached(el, scope.isAttached)) {
                    bottom.scroll(el);
                }
            }
            scope.$watch(scrollIfGlued);
            $timeout(scrollIfGlued, 0, false);
            $window.addEventListener('resize', scrollIfGlued, false);
            $el.bind('scroll', function () {
                activationState.setValue(bottom.isAttached(el,scope.isAttached));
            });
            $el.bind('mouseenter mouseleave',function () {
                scope.isAttached   = true;
            },function () {
                scope.isAttached   = false;
            })
        }
    };
}]);
